﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Progresoft.Scheduler.RemoteInterface.Api;
using Progresoft.Scheduler.RemoteInterface;
using Progresoft.Scheduler.RemoteInterface.DataAccess;
using System.Globalization;
using System.Collections.Generic;

public partial class TaskInfo : System.Web.UI.Page
{
	/// <summary>
	/// Ключ задачи
	/// </summary>
	protected string TaskKey
	{
		get
		{
			return this.Request.QueryString["task"];
		}
	}

	/// <summary>
	/// Система
	/// </summary>
	protected string SystemName
	{
		get
		{
			return this.Request.QueryString["system"] == null ?
				string.Empty : this.Request.QueryString["system"].ToString();
		}
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			if (this.SystemName == string.Empty)
				throw new Exception("Не задан обязательный параметр system");

			if (string.IsNullOrEmpty(TaskKey))
				throw new Exception("не задан обазательный параметр task");

			IList<TaskSchedule> schedules;
			ScheduledTaskInfo task = this.GetServer(SystemName)
				.GetScheduledTaskInfo(SystemName, TaskKey, out schedules);
			listSchedules.DataSource = schedules;
			listSchedules.DataBind();
			this.litAssembly.Text = task.Assembly;
			this.litClassName.Text = task.Class;
			this.litDescription.Text = task.Description;
			this.litKey.Text = task.Key;
			this.litTaskName.Text = task.TaskName;

			this.lnkNewSchedule.NavigateUrl = string.Format("~/EditSchedule.aspx?system={1}&task={0}",
				TaskKey, task.System);
		}
	}

	/// <summary>
	/// Получить строковое предстваление периода запуска
	/// </summary>
	protected string GetExecuteTime(TaskSchedule schedule)
	{
		switch (schedule.ScheduleType)
		{
			case TaskScheduleType.EveryDay:
				return string.Format("{0}, ежедневно", schedule.ExecutionTime.ToShortTimeString());

			case TaskScheduleType.EveryWeek:
				string[] days = ((DateTimeFormatInfo)new CultureInfo("ru-RU").DateTimeFormat).AbbreviatedDayNames;
				string day = days[(int)schedule.ExecutionDayOfWeek.Value];

				return string.Format("{0}, {1}", schedule.ExecutionTime.ToShortTimeString(), day);

			case TaskScheduleType.EveryMonth:
				return string.Format("{0}, каждый {1} день {2} месяца",
					schedule.ExecutionTime.ToShortTimeString(), schedule.ExecutionDayNumber.Value,
					schedule.ExecutionDayIsMonthBegin.Value ? "от начала" : "от конца");

			default:
				return string.Empty;
		}
	}

	protected string GetRowCss(TaskEventState status)
	{
		switch (status)
		{
			case TaskEventState.Failed:
			case TaskEventState.Cancelled:
			case TaskEventState.Delayed:
				return "schedulerTableRed";
			case TaskEventState.Created:
				return "schedulerTableGray";
			default:
				return string.Empty;
		}
	}

	protected string GetScheduledTaskStatusIcon(TaskEventState status)
	{
		return string.Format("~/Images/scheduler/status_{0}.gif", status.ToString().ToLower());
	}

	protected void ds_ObjectDisposing(object sender, ObjectDataSourceDisposingEventArgs e)
	{
		e.Cancel = true;
	}

	protected void listSchedules_ItemCommand(object sender, ListViewCommandEventArgs e)
	{
		switch (e.CommandName)
		{
			case ("DeleteSchedule"):

				int id = Convert.ToInt32(e.CommandArgument);
				this.GetServer(SystemName)
					.DeleteSchedule(id);
				Response.Redirect(Request.RawUrl);
				break;
		}
	}

	protected SchedulerServer GetServer(string system)
	{
		string url = ConnectorManager.GetServerUrl(SystemName);
		return ConnectorManager.GetConnector().GetServer(url);
	}
}
